const { success, failure } = require("../../utils/responses");
const { Attachment } = require("../../models");
const { NotFoundError } = require("../../utils/errors");
const fs = require("fs");

/**
 * 获取附件列表
 * @param {Omit<FindAndCountOptions<Attributes<M>>, "group">} query
 */
exports.getAttachmentList = async (query) => {
  try {
    return await Attachment.findAndCountAll(query);
  } catch (error) {
    throw new Error(error);
  }
};

/**
 * 保存附件信息
 * @param body
 */
exports.saveAttachment = async (body) => {
  try {
    return await Attachment.create(body);
  } catch (error) {
    throw new Error(error);
  }
};

/**
 * 修改附件信息
 * @param id
 * @param body
 */
exports.updateAttachment = async (id, body) => {
  try {
    const data = await this.getAttachmentById(id);
    return await data.update(body);
  } catch (error) {
    throw new Error(error);
  }
};

/**
 * 删除 id 附件信息
 * @param id
 */
exports.deleteAttachmentById = async (id) => {
  try {
    const data = await this.getAttachmentById(id);
    await data.destroy();
  } catch (error) {
    throw new Error(error);
  }
};

/**
 * 删除 businessId 附件信息
 * @param businessId
 */
exports.deleteAttachmentByBusinessId = async (businessId) => {
  try {
    // 删除附件
    await Attachment.destroy({
      where: {
        businessId: businessId,
      },
    });
  } catch (error) {
    throw new Error(error);
  }
};

/**
 * 删除 id 附件信息，同时删除文件信息
 * @param id
 */
exports.deleteFileAttachmentById = async (id) => {
  try {
    const data = await this.getAttachmentById(id);
    // 静态路径
    const staticDir = process.env.STATIC_DIR;
    const filePath = staticDir + data.filePath;
    // 判断文件是否存在
    if (fs.existsSync(filePath)) {
      // 删除文件
      fs.unlinkSync(filePath);
    }
    await data.destroy();
  } catch (error) {
    throw new Error(error);
  }
};

/**
 * 删除 businessId 附件信息，同时删除文件信息
 * @param businessId
 * @returns {Promise<void>}
 */
exports.deleteFileAttachmentByBusinessId = async (businessId) => {
  try {
    const rows = await Attachment.findAll({
      where: {
        businessId: businessId,
      },
    });
    if (rows && rows.length === 0) return;
    // 删除文件
    rows.forEach((row) => {
      // 静态路径
      // 判断文件是否存在
      if (fs.existsSync(row.filePath)) {
        // 删除文件
        fs.unlinkSync(row.filePath);
      }
    });
    // 删除附件
    await Attachment.destroy({
      where: {
        businessId: businessId,
      },
    });
  } catch (error) {
    throw new Error(error);
  }
};

/**
 * 根据ID获取信息
 * @param id
 * @returns {Promise<Model<any, TModelAttributes>>}
 */
exports.getAttachmentById = async (id) => {
  try {
    return await Attachment.findByPk(id);
  } catch (error) {
    throw new Error(error);
  }
};

/**
 * 根据businessId获取信息
 * @param businessId
 */
exports.getBusinessIdList = async (businessId) => {
  try {
    return await Attachment.findAll({
      raw: true,
      where: {
        businessId: businessId,
      },
    });
  } catch (error) {
    throw new Error(error);
  }
};
